import java.util.Stack;

//有效的括号
//输入：s = "()"
//输出：true
class Solution1 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //1.遍历字符串
        for(int i = 0;i<s.length();i++){
            char ch = s.charAt(i);
            //2.判断是不是左括号
            if(ch == '{' || ch == '[' || ch == '('){
                stack.push(ch);//入栈
            }else{
                //3.遇到右括号
                if(stack.empty()){
                    return false;
                }
                char ch2 = stack.peek();//看看是哪个左括号,ch是左括号，ch2是右括号
                if((ch2 == '{' && ch == '}') || (ch2 == '[' && ch == ']') || (ch2 == '(' && ch == ')')){
                    stack.pop();//让这个匹配的左括号出栈
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
         return true;
    }
}